package org.vectrics.recruiter.struts.tag;

/**
 * @author Mike Moore
 */

import org.apache.ecs.html.IMG;
import org.vectrics.SystemException;
import org.vectrics.common.html.*;
import org.vectrics.common.util.StringUtils;
import org.vectrics.common.view.FieldTable;
import org.vectrics.company.*;
import org.apache.log4j.Logger;
import org.vectrics.location.Address;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.struts.VectricsTagSupport;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;

public class CompanyViewTag extends VectricsTagSupport {

	private static final long serialVersionUID = 8014005747997627627L;
	
	static private transient Logger log = Logger.getLogger(CompanyViewTag.class);
    private int lastBgClass = 0;
    private FieldTable fieldTable = new FieldTable();

    public String makeStartTag()  {
		log.debug("doStartTag() called - class = " + this.getClass());
		
    	HttpServletRequest request = (HttpServletRequest)super.pageContext.getRequest();
		HttpSession session = super.pageContext.getSession();
        
        Table pageTable = new Table();
        try {
	        pageTable.setWidth(600);
	        Row pageRow = pageTable.addRow();
	        Cell leftCell = pageRow.addCell();
	        //leftCell.setBgColor("#f8f8f0");
	        Cell sepCell = pageRow.addCell();
	        sepCell.addElement("&nbsp;");
	        sepCell.setWidth(20);
	        Cell rightCell = pageRow.addCell();
	        //rightCell.setBgColor("#eaeaea");
	        rightCell.setWidth(200);
	        IMG clear = new IMG();
	        clear.setSrc(request.getContextPath() + "/graphics/cleardot.gif");
	        clear.setWidth(200);
	        clear.setHeight(1);
	        rightCell.addElement(clear);
	        rightCell.addElement("<br>");
	        
	        
	        //userProfile = RecruitManager.getUserProfile(request);
	        
	        log.debug("doRender() - getting http params");
	        Company company = null;
	        String companyId = request.getParameter("companyId");
	        if (StringUtils.isNullOrBlank(companyId)) {
		        companyId = request.getParameter("id");
	        }
	       
	        if ((StringUtils.isNullOrBlank(companyId)) && (request.getAttribute("companyId")!=null)){
		        companyId = (String) request.getAttribute("companyId");
	        }
	        
	        if (companyId == null) {
				log.error("doRender() - 'company' param not given");
	            throw new JspException("Company not found.  ID = " + companyId);
	        }
	        else {
	            log.debug("doRender() - 'company' param given looking for company.");
	            company = CoreServiceLocator.getCompanyService().findCompany(new Long(companyId));
	            if (company == null) {
	                log.error("doRender() - could not find company param: " + companyId);
	            }
	            else {
	            	log.debug("doRender() - found company.");
	                fieldTable = new FieldTable();
	                fieldTable.setCellPadding(2);
	                fieldTable.setWidth(500);
	                fieldTable.setValueCellWidth("350");
	                leftCell.addElement(fieldTable);
	
	                
	                try {
		                if (company.getParent() != null) {
		                	addFieldRow(this.getPhrase("prompt.company.name"), company.getParent().getName());
		                	addFieldRow(this.getPhrase("prompt.company.division.name"), company.getName());
		                }
		                else {
		                	addFieldRow(this.getPhrase("prompt.company.name"), company.getName());
		                }
	                }
	                catch (Exception ex) {
	                	log.error("Exception displaying parent", ex);
	                }
	                addFieldRow(this.getPhrase("prompt.company.reference.id"), company.getReferenceId());
                	addFieldRow(this.getPhrase("prompt.company.sector"), this.getPhrase(company.getSector().getDescriptionCode()));
	                addFieldRow(this.getPhrase("prompt.company.phone"), company.getPhone());
	                
	                Address address = company.getAddress();
	                if (address == null) {
	                	addFieldRow(this.getPhrase("prompt.company.address"), this.getPhrase("phrase.not.given"));
	                }
	                else {
	                	String addressHtml = "";
	                	if (address.getAddress1() != null)
	                		addressHtml += address.getAddress1();
	                	if (address.getAddress2() != null) {
	                		addressHtml += "<br>";
	                		addressHtml += address.getAddress2();
	                	}
	                	if (address.getCity() != null) {
	                		addressHtml += "<br>";
	                		addressHtml += address.getCity();
	                	}
	                	if (address.getState() != null) {
	                		addressHtml += "<br>";
	                		addressHtml += address.getState();
	                	}
	                	if (address.getPostalCode() != null) {
	                		addressHtml += "<br>";
	                		addressHtml += address.getPostalCode();
	                	}
	                	addFieldRow(this.getPhrase("prompt.company.address"), addressHtml);
	                }
		            addExtendedInformation(company);
		            log.debug("If the base configuration has multiple data owners, then display the data owner for the company");
		            if (CoreServiceLocator.getConfiguration().getSingleDomain(CoreServiceLocator.getConfiguration().getSystemDomain()) == false)
		            	addFieldRow(this.getPhrase("prompt.domain.name"), company.getDomain().getDescription());
	            }
	        }
	        log.debug("doRender() - done.");
        }
        catch (Exception ex) {
        	log.error("Exception in CompanyViewTag ", ex);
        	throw new SystemException("Exception in CompanyViewTag ", ex);
        }
	
		return(pageTable.toString());
    }

    protected void addExtendedInformation(Company company) {
        log.debug("base addExtendedInformation() - not overriden by derived class");
    }
    
    protected void addFieldRow(String caption, String value) {
    	this.fieldTable.showField(caption, value);
    }
}
